package com.gitee.aurora.core.comm;

import com.gitee.aurora.core.utils.MapToObj;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class AuroraNamedParameterJdbcTemplate extends NamedParameterJdbcTemplate {

    public AuroraNamedParameterJdbcTemplate(DataSource dataSource) {
        super(dataSource);
    }

    public <T> List<T> queryForMapToList(String sql, Map<String,Object> args, Class<T> elementType) {
        List<T> arrayList = new ArrayList<>();
        List<Map<String, Object>> maps = super.queryForList(sql,args);
        for (Map<String, Object> m : maps) {
            T t = MapToObj.mapToObj(m, elementType);
            arrayList.add(t);
        }
        return arrayList;
    }
}
